package com.ygo.feihua.adapter;
import android.support.v7.widget.*;
import com.ygo.feihua.*;
import android.view.View.*;
import android.content.*;
import android.view.*;
import com.ygo.feihua.type.*;
import java.util.*;
import android.widget.*;

public class AdapterRobot extends RecyclerView.Adapter<AdapterRobot.ViewHolder>
{
	
	
	private static final int MESSAGE_ROBOT = 0;
	private static final int MESSAGE_WO = 1;

	@Override
	public int getItemViewType(int position)
	{
		//根据不同position返回不同布局
		if(data.get(position).getType()==0){
			return MESSAGE_ROBOT;
		}else{
			return MESSAGE_WO;
		}
	}
	
	@Override
	public void onBindViewHolder(AdapterRobot.ViewHolder p1, int position)
	{
		switch(getItemViewType(position)){
			case MESSAGE_ROBOT:
				
				p1.icon.setImageResource(R.drawable.feihua);

				String sendmessage=data.get(position).getMsg();
				int start=sendmessage.indexOf("[kp");
				int end=-1;
				if(start!=-1){
					end=sendmessage.indexOf("]",start);
				}
				if(start!=-1&&end!=-1){

					p1.msg.setText(sendmessage.substring(end+1,sendmessage.length()));

					final String km=sendmessage.substring(start+3,end);
					//	zujian.kt.setImageBitmap(gj.getkt(km));

					Gongju.setKt(context,p1.kt,km);
					p1.kt.setVisibility(View.VISIBLE);
					p1.kt.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								/*Intent i=new Intent(context, A5.class);
								i.putExtra("km", km);
								context.startActivity(i);*/
								// TODO: Implement this method
							}
						});
				}else{
					if(data.get(position).getImage_url()!=null){	
						gj.tuxian(data.get(position).getImage_url(),p1.kt);
						p1.kt.setVisibility(View.VISIBLE);
					}else{
						p1.kt.setVisibility(View.GONE);
					}
					p1.msg.setText(sendmessage);
				}




				break;			
			case MESSAGE_WO:
				
				p1.kt.setVisibility(View.GONE);
				p1.msg.setText(data.get(position).getMsg());
				gj.setTouxiang(p1.icon,data.get(position).getUser());


				break;
		}
		p1.name.setText(data.get(position).getName());
		
		// TODO: Implement this method
	}
	
	List<RobotLiaotian> data;
	Context context;
	Gongju gj;
	
	
	

	@Override
	public AdapterRobot.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		
		View v = null;
		switch(p2){
			case MESSAGE_WO:
				v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_lt_wo, p1,false);
				break;
			case MESSAGE_ROBOT:
				v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_lt_robot, p1,false);
				break;
		}
		// TODO: Implement this method
		return new ViewHolder(v);
		
	}
	
	
	public AdapterRobot(Context context,List<RobotLiaotian> data){
		this.context=context;
		this.data=data;
		gj=Gongju.getdx(context);
	}
	
	
	public void sx(RobotLiaotian ro){
		data.add(ro);
		notifyDataSetChanged();
	}
	
	@Override
	public int getItemCount()
	{
		// TODO: Implement this method
		return data.size();
	}
	
	 class ViewHolder extends RecyclerView.ViewHolder
	{
		TextView name,msg;
		ImageView icon,kt;
		View vh;

		public ViewHolder(View v){
			super(v);
			vh=v;
			icon = (ImageView) v.findViewById(R.id.icon);

			name=(TextView) v.findViewById(R.id.name);

			kt=(ImageView) v.findViewById(R.id.kt);
			msg=(TextView) v.findViewById(R.id.msg);

		}
	}
	
	
}
